package cn.homework.transactionMessge;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class TransactionConsumer {
    public static void main(String[] args) throws Exception {
        // 实例化消费者Consumer
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("TransactionMessage-Consumer"/*消费者组名*/);
        // 地址
        consumer.setNamesrvAddr("127.0.0.1:9876");
        // 设置消费组群
        consumer.subscribe("Transaction-message","*");
        // 设置事务监听器
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                try {
                    list.forEach(list1-> System.out.println(new String(list1.getBody())));
                    System.out.println(consumeConcurrentlyContext.toString());
                    // 成功消费
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                } catch (Exception e) {
                    e.printStackTrace();
                    // 重新消费
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
            }
        });
        // 启动消费者
        consumer.start();

    }



}
